/*
 * Copyright (c) 2024 huipei.x
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.xphsc.kubernetes.core.executor;


import cn.xphsc.kubernetes.core.query.ReadNamespaceQuery;
import cn.xphsc.kubernetes.core.query.ReplaceNamespaceBody;
import cn.xphsc.kubernetes.utils.Asserts;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.AppsV1Api;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.*;
import org.apache.commons.lang3.StringUtils;

/**
 * {@link }
 * @author <a href="xiongpeih@163.com">huipei.x</a>
 * @description: Replace Executor
 * @since 1.0.3
 */
public class ReplaceNamespaceExecutor<T> extends AbstractExecutor<T> {
    private String ReplaceOptionName;
    private  T entity;
    private  CoreV1Api  coreV1Api;
    private ReplaceNamespaceBody replaceNamespaceBody;
    private AppsV1Api appsv1api;
    public ReplaceNamespaceExecutor(ApiClient client, ReplaceNamespaceBody replaceNamespaceBody, ReplaceOptionMode replaceOptionMode) {
        super(client);
        this.ReplaceOptionName=replaceOptionMode.name();
        this.coreV1Api=new CoreV1Api(this.getClient());
        this.appsv1api=new AppsV1Api(this.getClient());
        this.replaceNamespaceBody=replaceNamespaceBody;
    }

    @Override
    protected  T doExecute() {
        if (StringUtils.isNotBlank(ReplaceOptionName)) {
            switch (ReplaceOptionName){
                case "NS":
                    try {
                        Asserts.isInstanceOf(V1Namespace.class,replaceNamespaceBody.body());
                        entity = (T) coreV1Api.replaceNamespace(replaceNamespaceBody.name(), (V1Namespace) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SERVICE":
                    try {
                        Asserts.isInstanceOf(V1Service.class,replaceNamespaceBody.body());
                        entity = (T) coreV1Api.replaceNamespacedService(replaceNamespaceBody.name(),replaceNamespaceBody.namespace(), (V1Service) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "DEPLY":
                    try {
                        Asserts.isInstanceOf(V1Deployment.class,replaceNamespaceBody.body());
                        entity = (T) appsv1api.replaceNamespacedDeployment(replaceNamespaceBody.name(),replaceNamespaceBody.namespace(), (V1Deployment) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SECRET":
                    try {
                        Asserts.isInstanceOf(V1Secret.class,replaceNamespaceBody.body());
                        entity = (T) coreV1Api.replaceNamespacedSecret(replaceNamespaceBody.name(),replaceNamespaceBody.namespace(), (V1Secret) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "CONFIGMAP":
                    try {
                        Asserts.isInstanceOf(V1ConfigMap.class,replaceNamespaceBody.body());
                        entity = (T) coreV1Api.replaceNamespacedConfigMap(replaceNamespaceBody.name(),replaceNamespaceBody.namespace(), (V1ConfigMap) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "POD":
                    try {
                        Asserts.isInstanceOf(V1Pod.class,replaceNamespaceBody.body());
                        entity = (T) coreV1Api.replaceNamespacedPod(replaceNamespaceBody.name(),replaceNamespaceBody.namespace(), (V1Pod) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ENDPOINTS":
                    try {
                        Asserts.isInstanceOf(V1Endpoints.class,replaceNamespaceBody.body());
                        entity = (T) coreV1Api.replaceNamespacedEndpoints(replaceNamespaceBody.name(),replaceNamespaceBody.namespace(), (V1Endpoints) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SERVICEACCOUNT":
                    try {
                        Asserts.isInstanceOf(V1ServiceAccount.class,replaceNamespaceBody.body());
                        entity = (T) coreV1Api.replaceNamespacedServiceAccount(replaceNamespaceBody.name(),replaceNamespaceBody.namespace(), (V1ServiceAccount) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "PODTEMPLATE":
                    try {
                        Asserts.isInstanceOf(V1PodTemplate.class,replaceNamespaceBody.body());
                        entity = (T) coreV1Api.replaceNamespacedPodTemplate(replaceNamespaceBody.name(),replaceNamespaceBody.namespace(), (V1PodTemplate) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "NODE":
                    try {
                        Asserts.isInstanceOf(V1Node.class,replaceNamespaceBody.body());
                        entity = (T) coreV1Api.replaceNode(replaceNamespaceBody.name(), (V1Node) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "PERSISTENTVOLUME":
                    try {
                        Asserts.isInstanceOf(V1PersistentVolume.class,replaceNamespaceBody.body());
                        entity = (T) coreV1Api.replacePersistentVolume(replaceNamespaceBody.name(), (V1PersistentVolume) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "EVENT":
                    try {
                        Asserts.isInstanceOf(CoreV1Event.class,replaceNamespaceBody.body());
                        entity = (T) coreV1Api.replaceNamespacedEvent(replaceNamespaceBody.name(),replaceNamespaceBody.namespace(), (CoreV1Event) replaceNamespaceBody.body(),replaceNamespaceBody.pretty(),replaceNamespaceBody.dryRun(),replaceNamespaceBody.fieldManager(),replaceNamespaceBody.fieldValidation());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                default:

            }
        }
        return (T) entity;
    }
    public static enum ReplaceOptionMode {
        /**
         *
         */
        NS,
        SERVICE,
        ENDPOINTS,
        DEPLY,
        SECRET,
        ASYNC,
        SERVICEACCOUNT,
        CONFIGMAP,
        POD,
        PODTEMPLATE,
        NODE,
        PERSISTENTVOLUME,
        EVENT,
        AUTO;
        private ReplaceOptionMode() {

        }
        private ReplaceOptionMode(String name) {
            this.name=name;
        }
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}


